//
//  WelcomeViewController.m
//  Weibo
//
//  Created by student on 16/5/4.
//  Copyright © 2016年 Mcc. All rights reserved.
//

#import "WelcomeViewController.h"
#import "UserAccount.h"
#import <UIImageView+WebCache.h>
// 定义之后可以不用带mas_前缀
#define MAS_SHORTHAND
// 定义之后equalTo等于mas_equalTo
#define MAS_SHORTHAND_GLOBALS

#import <Masonry.h>

@interface WelcomeViewController ()
//背景图片
@property (nonatomic,strong)UIImageView *bgImageView;
//icon图片
@property(nonatomic,strong)UIImageView *iconImageView;
//欢迎文字
@property(nonatomic,strong)UILabel *welcomeLabel;

@end

@implementation WelcomeViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}
-(void)setupUI
{
    //1.添加子控件
    [self.view addSubview:self.bgImageView];
    [self.view addSubview:self.iconImageView];
    [self.view addSubview:self.welcomeLabel];
    
    //2.添加布局约束
    [self.bgImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.top.equalTo(self.view);
    }];
    
    [self.iconImageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.top.equalTo(self.view.top).offset(150);
        make.width.height.equalTo(100);
    }];
    
    [self.welcomeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.top.equalTo(self.iconImageView.bottom).offset(30);
    }];
    //3.设置用户的头像
    UserAccount *account=[UserAccount sharedUserAccount];
    if([account isLogined] && account.avatar_large.length > 0)
    {
        
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:account.avatar_large]];
    }
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self performSelector:@selector(startAnimation) withObject:nil afterDelay:2];
    
  }
- (void)startAnimation
{
    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        //        self.iconView.transform = CGAffineTransformMakeScale(2, 2);
        [self.iconImageView updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(-100);
        }];
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kNontificationRootSwitchViewController object:nil userInfo:@{@"VC":@"Main"}];
    }];
}
#pragma mark - 懒加载子控件
-(UIImageView *)bgImageView
{
    if (_bgImageView == nil)
    {
        _bgImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ad_background"]];
    }
    return _bgImageView;
}

-(UIImageView *)iconImageView
{
    if (_iconImageView == nil)
    {
        _iconImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"avatar_default_big"]];
        _iconImageView.layer.cornerRadius=50;
        _iconImageView.clipsToBounds=YES;
    }
    return _iconImageView;
}

-(UILabel *)welcomeLabel
{
    if (_welcomeLabel==nil) {
        _welcomeLabel=[[UILabel alloc]init];
        _welcomeLabel.text=@"欢迎回来";
        [_welcomeLabel sizeToFit];
    }
    return _welcomeLabel;
}
@end
